package com.atguigu.testdemo;

import com.atguigu.Collection.Collection;
import com.atguigu.Collection.Library;
import com.atguigu.bean.Dept;
import com.atguigu.bean.Staff;
import com.atguigu.factoryBean.Bean1;
import com.atguigu.factoryBean.MyBean;
import com.atguigu.service.UserService;
import com.atguigu.singleton_prototype.Book5;
import com.atguigu.singleton_prototype.Book6;
import com.atguigu.spring5.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class TestSpring5 {

    /**
     * 测试：通过 “配置文件” 来 “创建对象” 并调用对象中的方法
     */
    @Test
    public void test1() {
        //加载spring 配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //获取配置创建的对象
        User user = context.getBean("user", User.class);
        //调用对象方法并打印
        System.out.println(user);
        user.add();
    }

    /**
     * 测试：通过配置文件给类的属性，注入属性值（利用set方法注入）
     */
    @Test
    public void test2() {
        //加载spring 配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //获取配置创建的对象
        Book book = context.getBean("Book", Book.class);
        System.out.println(book);
    }

    /**
     * 测试: 通过配置文件给类的属性，注入属性值（利用有参构造器注入）
     */
    @Test
    public void test3() {
        //加载spring 配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //获取配置创建的对象
        Order order = context.getBean("Order", Order.class);
        System.out.println(order);
    }

    /**
     * 测试： 利用p空间给类的属性，注入属性值
     */
    @Test
    public void test4() {
        //加载spring 配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //获取配置创建的对象
        Person person = context.getBean("Person", Person.class);
        System.out.println(person);
    }

    /**
     * 测试: 给类中属性，注入特殊字符的方法
     */
    @Test
    public void test5() {
        //加载spring 配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //获取配置创建的对象
        Book2 book2 = context.getBean("Book2", Book2.class);
        System.out.println(book2);
    }

    /**
     * 测试： 利用外部bean，给对象类型属性注入值
     */
    @Test
    public void test6() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("UserService", UserService.class);
        userService.show();
    }

    /**
     * 测试： 利用内部bean，给对象类型属性注入值
     */
    @Test
    public void test7() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Staff staff = context.getBean("Staff", Staff.class);
        System.out.println(staff);
    }

    /**
     * 测试： 级联赋值(一)
     */
    @Test
    public void test8() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        Staff staff = context.getBean("Staff", Staff.class);
        System.out.println(staff);
    }

    /**
     * 测试： 多级级联赋值(二)
     */
    @Test
    public void test9() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Staff staff = context.getBean("Staff", Staff.class);
        System.out.println(staff);
    }

    /**
     * 测试： 给集合数组(包括list,set,map集合)注入属性值
     */
    @Test
    public void test10() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        Collection collection = context.getBean("Collection", Collection.class);
        System.out.print("int类型数组 arr1:");
        collection.showArr1();
        System.out.print("String类型数组 arr:");
        collection.showArr();
        System.out.print("list类型数组 list:");
        collection.showList();
        System.out.print("map类型数组 map:");
        collection.showMap();
        System.out.print("set类型数组 set:");
        collection.showSet();
//        System.out.print("ArrayList类型数组 ArrayList:");collection.showArrayList();
    }

    /**
     * 测试： 给集合数组(包括list,set,map集合)注入"对象属性值"
     */
    @Test
    public void test11() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        Library library = context.getBean("Library", Library.class);
        System.out.println(library);
    }

    /**
     * 测试： 把集合注入部分提取出来,给集合数组(包括list,set,map集合)注入"对象属性值"
     */
    @Test
    public void test12() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
        Library library = context.getBean("Library", Library.class);
        System.out.println(library);
    }

    /**
     * 测试: 利用工厂bean,获取不同类型的bean对象
     */
    @Test
    public void Test13() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");
        Bean1 bean1 = context.getBean("MyBean", Bean1.class);
        System.out.println(bean1);

    }

    /**
     * 测试: 配置的bean对象为单例对象,或者多例对象
     */
    @Test
    public void Test14() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
        Book5 book1 = context.getBean("Book5", Book5.class);
        Book5 book2 = context.getBean("Book5", Book5.class);
        //单例测试,每次从配置对象中获取的对象都是同一个,因此他们的地址都是相同的
        System.out.println(book1);
        System.out.println(book2);
        System.out.println("---------------------------------");
        //多例模式,可以每次从配置对象中获取不同地址的对象
        ApplicationContext context2 = new ClassPathXmlApplicationContext("bean7.xml");
        Book6 book3 = context.getBean("Book6", Book6.class);
        Book6 book4 = context.getBean("Book6", Book6.class);
        System.out.println(book3);
        System.out.println(book4);
    }

    @Test
    public void test0() {
        List list = new ArrayList();
        list.add(null);
        list.add("hello");
        list.add(null);
        System.out.println(list);
    }
}
